package com.filter;

import com.lagou.bean.MethodInvokeTime;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author wyx
 * @Date 2020/4/15 23:25
 */

@Activate(group = {CommonConstants.CONSUMER,CommonConstants.PROVIDER})
public class TPMonitorFilter implements Filter {
    private static long startTime;
    private static long useTime;
    private static List<MethodInvokeTime> MethodAInvokeTimes = new LinkedList<MethodInvokeTime>();
    private static List<MethodInvokeTime> MethodBInvokeTimes = new LinkedList<MethodInvokeTime>();
    private static List<MethodInvokeTime> MethodCInvokeTimes = new LinkedList<MethodInvokeTime>();

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        startTime  = System.currentTimeMillis();
        try {
            // 执行方法
            return  invoker.invoke(invocation);
        } finally {
            useTime=System.currentTimeMillis()-startTime;
//            System.out.println(invocation.getMethodName()+" invoke time:"+useTime + "毫秒");

            if (invocation.getMethodName().equals("methodA")){

                MethodAInvokeTimes.add(new MethodInvokeTime(useTime,startTime));
                while (System.currentTimeMillis()-MethodAInvokeTimes.get(0).getInvokeTime()>60000){
                    MethodAInvokeTimes.remove(0);
                }
            }else
            if (invocation.getMethodName().equals("methodB")){

                MethodBInvokeTimes.add(new MethodInvokeTime(useTime,startTime));
                while (System.currentTimeMillis()-MethodBInvokeTimes.get(0).getInvokeTime()>60000){
                    MethodBInvokeTimes.remove(0);
                }

            }else
            if (invocation.getMethodName().equals("methodC")){

                MethodCInvokeTimes.add(new MethodInvokeTime(useTime,startTime));

                while (System.currentTimeMillis()-MethodCInvokeTimes.get(0).getInvokeTime()>60000){
                    MethodCInvokeTimes.remove(0);
                }
            }


        }

    }

    public static List<MethodInvokeTime> getMethodAInvokeTimes() {
        return MethodAInvokeTimes;
    }

    public static void setMethodAInvokeTimes(List<MethodInvokeTime> methodAInvokeTimes) {
        MethodAInvokeTimes = methodAInvokeTimes;
    }

    public static List<MethodInvokeTime> getMethodBInvokeTimes() {
        return MethodBInvokeTimes;
    }

    public static void setMethodBInvokeTimes(List<MethodInvokeTime> methodBInvokeTimes) {
        MethodBInvokeTimes = methodBInvokeTimes;
    }

    public static List<MethodInvokeTime> getMethodCInvokeTimes() {
        return MethodCInvokeTimes;
    }

    public static void setMethodCInvokeTimes(List<MethodInvokeTime> methodCInvokeTimes) {
        MethodCInvokeTimes = methodCInvokeTimes;
    }

    public static void main(String[] args) throws InterruptedException {

       /* List<String> list = new LinkedList<String>();

        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");

        for (String s:list) {
            System.out.println(s);
        }

        while (list.size()>2){
            list.remove(0);
        }

        for (String s:list) {
            System.out.println(s);
        }*/

        long a = System.currentTimeMillis();
        Thread.sleep(30000);
        long b = System.currentTimeMillis();
        long c = b-a;
        System.out.println(b+"-"+a+"="+c);
    }
}
